<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Services\WxMassService;

/**
 * 群发结果
 * @package App\Http\Controllers\Admin
 */
class WxMassResultController extends BaseController
{
    private $wxMassSer;
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->wxMassSer = new WxMassService();
    }

    /**
     * 群发结果列表页
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        if ($request->ajax()) {
            $pageSize = $request->get('pageSize', 10);
            $query = \App\Models\WxMass::has('result')->with('result')->with('articles')->select('*');
            $keyword = $request->get('keyword', '');
            if ($keyword) {
                $query->where('title', 'like', '%' . $keyword . '%');
            }
            $data = $query->paginate($pageSize)->toArray();
            if ($data['data']){
                $replyList = $data['data'];
                foreach ($replyList as $k => $v){
                    $v['msg_type_str'] = $this->wxMassSer->getMsgTypeStr($v['msg_type']);
                    $replyList[$k] = $v;
                }
                $data['data'] = $replyList;
            }

            return $data;
        }
        return view('admin.wxmassresult.index', $this->viewData);
    }
}
